package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.ILegalAreaCityService;
import com.mdd.admin.validate.LegalAreaCityCreateValidate;
import com.mdd.admin.validate.LegalAreaCityUpdateValidate;
import com.mdd.admin.validate.LegalAreaCitySearchValidate;
import com.mdd.admin.vo.LegalAreaCityListedVo;
import com.mdd.admin.vo.LegalAreaCityDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.LegalAreaCity;
import com.mdd.common.mapper.LegalAreaCityMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 归属地实现类
 * @author LikeAdmin
 */
@Service
public class LegalAreaCityServiceImpl implements ILegalAreaCityService {
        
    @Resource
    LegalAreaCityMapper legalAreaCityMapper;

    /**
     * 归属地列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<LegalAreaCityListedVo>
     */
    @Override
    public PageResult<LegalAreaCityListedVo> list(PageValidate pageValidate, LegalAreaCitySearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<LegalAreaCity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("area_type", 2);
        queryWrapper.orderByAsc("area_id");

        legalAreaCityMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:areaName@area_name:str",
            "=:parentId@parent_id:long",
        });

        IPage<LegalAreaCity> iPage = legalAreaCityMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<LegalAreaCityListedVo> list = new LinkedList<>();
        for(LegalAreaCity item : iPage.getRecords()) {
            LegalAreaCityListedVo vo = new LegalAreaCityListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    @Override
    public List<LegalAreaCityListedVo> allProvinceOrCity(Integer type) {
        // type 1-省份 2 -城市
        QueryWrapper<LegalAreaCity> wrapper = new QueryWrapper<>();
        wrapper.eq("area_type", type);
        if (type == 2) {
            wrapper.orderByAsc("area_word");
        } else {
            wrapper.orderByAsc("area_id");
        }
        List<LegalAreaCity> list = legalAreaCityMapper.selectList(wrapper);
        List<LegalAreaCityListedVo> list2 = new LinkedList<>();
        for(LegalAreaCity item : list) {
            LegalAreaCityListedVo vo = new LegalAreaCityListedVo();
            BeanUtils.copyProperties(item, vo);
            list2.add(vo);
        }
        return list2;
    }


    /**
     * 归属地详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return LegalAreaCity
     */
    @Override
    public LegalAreaCityDetailVo detail(Integer id) {
        LegalAreaCity model = legalAreaCityMapper.selectOne(
                new QueryWrapper<LegalAreaCity>()
                    .eq("area_id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        LegalAreaCityDetailVo vo = new LegalAreaCityDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 归属地新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(LegalAreaCityCreateValidate createValidate) {
        LegalAreaCity model = new LegalAreaCity();
        model.setAreaName(createValidate.getAreaName());
        model.setAreaType(createValidate.getAreaType());
        model.setParentId(createValidate.getParentId());
        model.setAreaWord(createValidate.getAreaWord());
        legalAreaCityMapper.insert(model);
    }

    /**
     * 归属地编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(LegalAreaCityUpdateValidate updateValidate) {
        LegalAreaCity model = legalAreaCityMapper.selectOne(
                new QueryWrapper<LegalAreaCity>()
                    .eq("area_id",  updateValidate.getAreaId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setAreaName(updateValidate.getAreaName());
        model.setAreaType(updateValidate.getAreaType());
        model.setParentId(updateValidate.getParentId());
        model.setAreaWord(updateValidate.getAreaWord());
        legalAreaCityMapper.updateById(model);
    }

    /**
     * 归属地删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        LegalAreaCity model = legalAreaCityMapper.selectOne(
                new QueryWrapper<LegalAreaCity>()
                    .eq("area_id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        legalAreaCityMapper.delete(new QueryWrapper<LegalAreaCity>().eq("area_id", id));
    }

}
